package com.huohuzhihui.web.controller.agent;

import com.huohuzhihui.common.annotation.Log;
import com.huohuzhihui.common.core.controller.BaseController;
import com.huohuzhihui.common.core.domain.AjaxResult;
import com.huohuzhihui.common.core.domain.entity.SysUser;
import com.huohuzhihui.common.core.domain.model.LoginUser;
import com.huohuzhihui.common.core.page.TableDataInfo;
import com.huohuzhihui.common.enums.BusinessType;
import com.huohuzhihui.common.utils.ServletUtils;
import com.huohuzhihui.common.utils.poi.ExcelUtil;
import com.huohuzhihui.framework.web.service.TokenService;
import com.huohuzhihui.merchant.domain.AgentCost;
import com.huohuzhihui.merchant.service.IAgentCostService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 消费记录Controller
 *
 * @author huohuzhihui
 * @date 2021-06-04
 */
@RestController
@RequestMapping("/agent/cost")
public class AgentCostController extends BaseController {
    @Autowired
    private IAgentCostService agentCostService;
    @Autowired
    private TokenService tokenService;

    /**
     * 查询消费记录列表
     */
    @PreAuthorize("@ss.hasPermi('agent:cost:list')")
    @GetMapping("/list")
    public TableDataInfo list(AgentCost agentCost) {
        //只查询当前商户的
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        SysUser sysUser = loginUser.getUser();
        agentCost.setAgentId(sysUser.getDeptId());
        startPage();
        List<AgentCost> list = agentCostService.selectAgentCostList(agentCost);
        return getDataTable(list);
    }

    /**
     * 导出消费记录列表
     */
    @PreAuthorize("@ss.hasPermi('agent:cost:export')")
    @Log(title = "消费记录", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(AgentCost agentCost) {
        List<AgentCost> list = agentCostService.selectAgentCostList(agentCost);
        ExcelUtil<AgentCost> util = new ExcelUtil<AgentCost>(AgentCost.class);
        return util.exportExcel(list, "cost");
    }

    /**
     * 获取消费记录详细信息
     */
    @PreAuthorize("@ss.hasPermi('agent:cost:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(agentCostService.selectAgentCostById(id));
    }

    /**
     * 新增消费记录
     */
    @PreAuthorize("@ss.hasPermi('agent:cost:add')")
    @Log(title = "消费记录", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody AgentCost agentCost) {
        return toAjax(agentCostService.insertAgentCost(agentCost));
    }

    /**
     * 修改消费记录
     */
    @PreAuthorize("@ss.hasPermi('agent:cost:edit')")
    @Log(title = "消费记录", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody AgentCost agentCost) {
        return toAjax(agentCostService.updateAgentCost(agentCost));
    }

    /**
     * 删除消费记录
     */
    @PreAuthorize("@ss.hasPermi('agent:cost:remove')")
    @Log(title = "消费记录", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(agentCostService.deleteAgentCostByIds(ids));
    }
}
